package com.plusfoursix.game;

import java.util.Vector;

//import android.util.Log;

import rokon.Rokon;
import rokon.Sprite;
import rokon.Texture;

public class GameThread implements Runnable {

	private static int MONSTER_SPEED_START = 200;
	private static final int HARDER_TIMER = 1000;
	private Player player;
	private Monster[] monsters = new Monster[5];
	private Background firstBackground = new Background();
	private Background secondBackground = new Background();

	long lastTrigger = 0;
	Rokon rokon;

	enum GameState {
		INIT, RUNNING, DEAD
	};

	GameState state = GameState.INIT;
	private int counter;

	public GameThread(Rokon rokon) {
		Globals.SCREEN_WIDTH = rokon.getWidth();
		Globals.SCREEN_HEIGHT = rokon.getHeight();

		player = new Player();
		firstBackground = new Background();
		secondBackground = new Background();

		Texture playerTexture;
		Texture bgTexture;
		
		bgTexture = rokon.createTextureFromResource(firstBackground.getGraphic());
		playerTexture = rokon.createTextureFromResource(player.getGraphic());

		Texture enemyTexture = rokon.createTextureFromResource(R.drawable.roid);

		rokon.prepareTextureAtlas();

		Sprite playerSprite = new Sprite((rokon.getWidth() / 2) - 35, rokon.getHeight() - playerTexture.getHeight() - 20, 73, 74);
		playerSprite.setTexture(playerTexture);
		rokon.addSprite(playerSprite, 1);

		player.setSprite(playerSprite);

		Sprite bgFirstSprite;
		bgFirstSprite = new Sprite(rokon.getWidth() / 2 - 200, rokon.getHeight() - rokon.getHeight(), 400, 600);
		bgFirstSprite.setTexture(bgTexture);
		rokon.addSprite(bgFirstSprite, 0);

		Sprite bgSecondSprite;
		bgSecondSprite = new Sprite(rokon.getWidth() / 2 - 200, rokon.getHeight() - rokon.getHeight() - 600, 400, 600);
		bgSecondSprite.setTexture(bgTexture);
		rokon.addSprite(bgSecondSprite, 0);

		firstBackground.setSprite(bgFirstSprite);
		secondBackground.setSprite(bgSecondSprite);

		for (int i = 0; i < monsters.length; i++) {
			monsters[i] = new Monster();

		}

		for (int i = 0; i < monsters.length; i++) {
			Sprite enemySprite = new Sprite((rokon.getWidth() / 2) - 35 + i * 2, 0 + enemyTexture.getHeight(), 50, 50);

			enemySprite.setTexture(enemyTexture);
			rokon.addSprite(enemySprite, 1);
			monsters[i].setSprite(enemySprite);
			monsters[i].init();
		}

		state = GameState.RUNNING;

		// InputHandler handles all your screen touches
		rokon.setInputHandler(new PlayerInputHandler(player));

	}

	public void run() {

		while (true) {
			long now = System.currentTimeMillis();

			if (now - lastTrigger > 10) {
				updateLogic();
				lastTrigger = now;
			}

		}

		// Usually a loop in here would handle your game logic,
		// but since we are just filling a blank screen it is
		// okay to leave it here.
	}

	private void updateLogic() {
		if (state == GameState.RUNNING) {
			player.logic();

			firstBackground.logic();
			secondBackground.logic();
			for (Monster monster : monsters) {
				monster.logic();
			}

			if (collision(player, monsters)) {
				state = GameState.DEAD;
			}

			collision(monsters, player.getShoots());

			createMonsters();
			counter++;
		}
	}

	private void collision(Monster[] monsters, Vector<Monster> shoots) {
		// TODO Auto-generated method stub

	}

	private void createMonsters() {
		if ((counter % MONSTER_SPEED_START) == 0) {
			//Log.d("test", "Maybe add new monster");
			// activate one monster if available
			for (int i = 0; i < monsters.length; i++) {

				if (!monsters[i].isActive()) {
					monsters[i].setActive(true);
					break;
				}
			}

		}

		if ((counter % HARDER_TIMER) == 0) {
			//Log.d("test", "Making it harder");
			MONSTER_SPEED_START--;
			if (MONSTER_SPEED_START < 50) {
				MONSTER_SPEED_START = 50;
			}
		}

	}

	private boolean collision(Player player, Monster[] monsters) {
		for (Monster monster : monsters) {
			if (collide(player, monster)) {
				return true;
			}
		}
		return false;
	}

	private boolean collide(Player player, Monster monster) {

		int left1, left2;
		int right1, right2;
		int top1, top2;
		int bottom1, bottom2;

		left1 = (int) player.getSprite().getX();
		left2 = (int) monster.getSprite().getX();
		right1 = left1 + (int) player.getSprite().getWidth();
		right2 = left2 + (int) monster.getSprite().getWidth();
		top1 = (int) player.getSprite().getY();
		top2 = (int) monster.getSprite().getY();
		bottom1 = top1 + (int) player.getSprite().getHeight();
		bottom2 = top2 + (int) monster.getSprite().getHeight();

		if (bottom1 < top2)
			return false;
		if (top1 > bottom2)
			return false;

		if (right1 < left2)
			return false;
		if (left1 > right2)
			return false;

		return true;
	}

}